﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.DAL.Repositories
{
    public partial class SellerInsuranceProductMappingsRepository
    {
        public List<int> GetAllInsuranceProductIDs(long sellerInsuranceProductID)
        {
            var sql =
                string.Format(
                    @"select distinct (case when t.InsuranceProductId is null then t.InsuranceProductId_B else t.InsuranceProductId end) as InsuranceProductId from
                                     (SELECT TOP 1000 a.[ID]
                                          ,[SellerInsuranceProductId]
                                          ,a.[InsuranceProductId]
                                          ,a.[InsuranceProductPackageID],b.InsuranceProductId as InsuranceProductId_B
                                      FROM [dbo].[SellerInsuranceProductMappings] A
                                      left join [dbo].[InsuranceProductPackageMapping] B
                                      ON A.InsuranceProductPackageID=b.InsuranceProductPackageId where SellerInsuranceProductId=@SellerInsuranceProductId) t");
            var sqlParameters = new List<SqlParameter>
            {
                new SqlParameter
                {
                    ParameterName = "SellerInsuranceProductId",
                    Value = sellerInsuranceProductID
                }
            };
            var tb = SqlQuery(sql, sqlParameters.ToArray());
            var list = new List<int>();
            if (tb != null && tb.Rows.Count > 0)
            {
                list = DataTableToListHelper<InsuranceProductPackageMapping>.ConvertToEntityList(tb).Select(o => o.InsuranceProductId).ToList();
            }
            return list;
        }

        public List<int> GetPackageInsuranceProductIDs(long sellerInsuranceProductID)
        {
            var sql =
                string.Format(
                    @"  SELECT b.InsuranceProductId 
                          FROM [dbo].[SellerInsuranceProductMappings] A
                          left join [dbo].[InsuranceProductPackageMapping] B
                          ON A.InsuranceProductPackageID=b.InsuranceProductPackageId where B.InsuranceProductId is not null and SellerInsuranceProductId=@SellerInsuranceProductId");
            var sqlParameters = new List<SqlParameter>
            {
                new SqlParameter
                {
                    ParameterName = "SellerInsuranceProductId",
                    Value = sellerInsuranceProductID
                }
            };
            var tb = SqlQuery(sql, sqlParameters.ToArray());
            var list = new List<int>();
            if (tb != null && tb.Rows.Count > 0)
            {
                list = DataTableToListHelper<InsuranceProductPackageMapping>.ConvertToEntityList(tb).Select(o => o.InsuranceProductId).ToList();
            }
            return list;
        }

    }
}